﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;

namespace work17
{
    class Program
    {
        static void Main(string[] args)
        {

            //方法一
            //string connStr = "server=.;database=dbtest;Uid=sa;Pwd=123456;";

            //var sqlConn = new SqlConnection(connStr);

            //sqlConn.Open();

            //string cmdText = "select * from Users";

            //SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConn);



            //var dr = sqlCmd.ExecuteReader();
            //Console.WriteLine(" 姓名 \t 班级\n");
            //while (dr.Read())
            //{
            //    Console.WriteLine(" {0}\t {1}\n", dr[1], dr[2]);
            //}

            //sqlConn.Close();


            //方法二
            string connStr = "server=.;database=dbtest;Uid=sa;Pwd=123456;";

            SqlConnection sqlConn = new SqlConnection(connStr);

            sqlConn.Open();

            string cmdText = "select * from Users";

            SqlDataAdapter sda = new SqlDataAdapter(cmdText, sqlConn);

            DataSet ds = new DataSet();

            sda.Fill(ds);

            foreach (DataTable table in ds.Tables)
            { 
                foreach (DataRow row in table.Rows)
                {
                    Console.Write("学号：");
                    foreach (object field in row.ItemArray)
                    {
                        Console.Write("{0}\n",field);
                        Console.WriteLine();
                    }
                    
                }
                
            }

        }
    }
}
